package com.ghh.app.monsterrush;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Disposable;

public abstract class MonsterActor extends Actor implements Disposable {
	public final static int		WALKING		= 0;
	public final static int		ATTACKED	= 1;
	public final static int		DYING		= 2;
	public final static int		DEAD		= 3;

	protected int				life		= 0;
	protected float				speed		= 0;
	protected MonsterRushGame	game;
	protected int				status		= MonsterActor.WALKING;

	protected StateAction		wakState;
	protected StateAction		attState;
	protected StateAction		desState;
	protected StateAction		currentState;

	public MonsterActor(MonsterRushGame game, int life, float speed) {
		this.game = game;
		this.life = life;
		this.speed = speed;
		wakState = createWalkState();
		attState = createAttackedState();
		desState = createDestroyState();
		onWalk();
	}

	public void drawFrame(SpriteBatch batch, TextureRegion textureRegion) {
		batch.draw(textureRegion, this.getX(), this.getY(), this.getWidth(), this.getHeight());
	}

	protected abstract StateAction createWalkState();

	protected abstract StateAction createAttackedState();

	protected abstract StateAction createDestroyState();
	
	public void onWalk() {
		status = MonsterActor.WALKING;
		currentState = wakState;
		currentState.start();
	}

	public void walk() {
		if (!game.isBlocked(this)) {
			float dy = speed * Gdx.graphics.getDeltaTime();
			setY(getY() + dy);
		}
	}

	public void onAttacked(WeaponActor weapon) {
		status = ATTACKED;
		life -= weapon.getPower();
		if (life > 0) {
			currentState = attState;
			currentState.start();
		} else {
			onDestroying();
		}
	}

	public void onDestroying() {
		status = DYING;
		currentState = desState;
		currentState.start();
	}

	public void onDestroyed() {
		status = DEAD;
	}

	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		currentState.act(Gdx.graphics.getDeltaTime());
		currentState.draw(batch);
	}
	
	public boolean isDestroyed() {
		return status == DEAD;
	}

	public boolean isAlive() {
		return status == WALKING || status == ATTACKED;
	}

	public boolean isDead() {
		return status == DYING || status == DEAD;
	}

	public int getStatus() {
		return status;
	}
}
